﻿// 功能：矩形结构
// 描述：
// 作者：温伟鹏
// 日期：2010/5/13 14:27:49

using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace MeizuSDK.Drawing
{
    /// <summary>
    /// 矩形结构
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        /// <summary>
        /// RECT左边缘的X坐标
        /// </summary>
        public int Left;
        /// <summary>
        /// RECT上边缘的Y坐标
        /// </summary>
        public int Top;
        /// <summary>
        /// 获取 x 坐标，该坐标是此 System.Drawing.Rectangle 结构的 
        /// System.Drawing.Rectangle.X 与 System.Drawing.Rectangle.Width属性值之和。
        /// </summary>
        public int Right;
        /// <summary>
        /// 获取 y 坐标，该坐标是此 System.Drawing.Rectangle 结构的 
        /// System.Drawing.Rectangle.Y 与 System.Drawing.Rectangle.Height属性值之和。
        /// </summary>
        public int Bottom;

        /// <summary>
        /// 构建RECT结构实例
        /// </summary>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="right"></param>
        /// <param name="bottom"></param>
        public RECT(int left, int top, int right, int bottom)
        {
            this.Left = left;
            this.Top = top;
            this.Right = right;
            this.Bottom = bottom;
        }
        /// <summary>
        /// 构建RECT结构实例
        /// </summary>
        /// <param name="rect"></param>
        public RECT(Rectangle rect)
        {
            Left = rect.Left;
            Top = rect.Top;
            Right = rect.Right;
            Bottom = rect.Bottom;
        }
        /// <summary>
        /// 将RECT结构转化为Rectangle结构
        /// </summary>
        /// <returns></returns>
        public Rectangle ToRectangle()
        {
            return new Rectangle(this.Left, this.Top, this.Right - this.Left, this.Bottom - this.Top);
        }
        /// <summary>
        /// 将Rectangle结构转化为RECT结构体
        /// </summary>
        /// <param name="rect"></param>
        /// <returns></returns>
        public static RECT FromRectangle(Rectangle rect)
        {
            return new RECT(rect.Left, rect.Top, rect.Right, rect.Bottom);
        }
        /// <summary>
        /// 根据左上角坐标与宽高构建RECT结构体
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static RECT FromXYWH(int x, int y, int width, int height)
        {
            return new RECT(x, y, x + width, y + height);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rect"></param>
        /// <returns></returns>
        public static implicit operator RECT(Rectangle rect)
        {
            return new RECT(rect);
        }
    }
}
